import { updateClient } from '../../../../backend/controllers/clients';
import api from '../../http/apis/apis'

export default {
    namespaced: true,
    //下面写请求
    state: {
        //保存所有客户
        ClientsPagedata: {
            currentPage: 1,
            pageSize: 3,
            total: 0,
            totalPages: 0,
            data: []
        },
        update: {},//修改
        sales:{},
        product:{},
        producttype:{}
    },
    mutations: {
        // 获取所有客户
        Clients(state, Clientsdata) {
            state.ClientsPagedata = Clientsdata
            console.log(state.ClientsPagedata);
        },
        setCurrent(state, newcurrent) {
            state.ClientsPagedata.currentPage = newcurrent
        },
        setPageSize(state, newpagesize) {
            state.ClientsPagedata.pageSize = newpagesize
        },
        // 获取某一个客户
        getclients(state, val) {
            state.update = val
            console.log(state.update);
        },
        // 获取销售
        getsales(state, val) {
            state.sales = val
        },
        // 获取产品
        getproduct(state, val) {
            state.product = val
        },
        // 获取险种
        getproducttype(state, val) {
            state.producttype = val
        },
    },
    actions: {
        // 获取所有客户
        async getClients(context) {
            const { data } = await api.cosutomers.getClients({
                currentPage: context.state.ClientsPagedata.currentPage,
                pageSize: context.state.ClientsPagedata.pageSize,
            })
            console.log(data);
            if (data.status) {
                context.commit("Clients", data.data)
            }
        },
        //新增客户
        async addClients(context, salesDate) {
            let { data } = await api.cosutomers.addClients(salesDate)
            console.log(data);
        },
        //删除客户
        async deletClient(context, _id) {
            const { data } = await api.cosutomers.deletClient({_id} )
            if (data.status) {
                context.dispatch("getClients")
            }
        },
        // 获取某一个客户
        async getOneClient(context, _id) {
            console.log(_id);
            const { data } = await api.cosutomers.getClientsById(_id)
            console.log(data);
            if (data.status) {
                context.commit("getclients", data.data)
            }
        },
        // 修改
        async updateClient(context, update) {
            const { data } = await api.cosutomers.changeCustomers(update)
            if (data.status) {
                context.dispatch("getClients")
            }
        },
        // 获取销售员
        async getSales(context, updates) {
            const { data } = await api.cosutomers.getSales(updates)
            console.log(222222, data);
            if (data.status) {
                context.commit("getsales",data.data.data)
            }
        },
        // 获取产品
        async getProduct(context, val) {
            const { data } = await api.cosutomers.getProduct(val)
            console.log(33333334,data.data.data);
            if (data.status) {
                context.commit("getproduct",data.data.data)
            }
        },
        // 获取险种
        async getProductType(context, updates) {
            const { data } = await api.cosutomers.getProductType(updates)
            console.log(22233333,data.data);
            if (data.status) {
                context.commit("getproducttype",data.data)
            }
        },
    }

}